package fr.fbhelp.files
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.filesystem.File;
	
	import fr.fbhelp.events.ParserEvent;
	import fr.fbhelp.parsing.ParseManager;
	
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;

	public class HelpIndexCleaner
	{
		private var eclipseHelpIndexFolder:File;
		
		public function HelpIndexCleaner()
		{
		}
		
		public function prepareEclipseHelpIndexDeletion(eclipseHelpIndexFoldr:File): void
		{
			eclipseHelpIndexFolder = eclipseHelpIndexFoldr; 
			if (eclipseHelpIndexFolder != null)
			{
				Alert.show("The Eclipse help index folder must be deleted now in order to make Flash Builder to re-create it after restart. " +
					"The Eclipse help index folder is: \n\n" + eclipseHelpIndexFolder.nativePath + "\n" +
					" \nCan I delete it now?",
					ParseManager.TITLE_DELETE_INDEX, (Alert.YES|Alert.NO), null, onDeleteHelpIndexAlertClose, null, Alert.YES);
			}
			else
			{
				Alert.show("The Eclipse help index folder is NULL", "Error");
			}
		}
		
		public function onDeleteHelpIndexAlertClose(event:CloseEvent): void
		{
			if (event.detail == Alert.YES)
			{
				deleteEclipseHelpIndex();
			}
			if (event.detail == Alert.NO)
			{
				Output.append("Eclipse help index folder deletion has been cancelled");
			}
		}
		
		public function deleteEclipseHelpIndex(): void
		{
			if (eclipseHelpIndexFolder.exists)
			{
				eclipseHelpIndexFolder.addEventListener(Event.COMPLETE, onEclipseHelpFolderDeletionComplete, false, 0, true);
				eclipseHelpIndexFolder.addEventListener(IOErrorEvent.IO_ERROR, onEclipseHelpFolderDeletionIOError, false, 0, true);
				
				eclipseHelpIndexFolder.deleteDirectoryAsync(true);
			}
			else
			{
				Output.append("Sorry, the folder does not exist: " + eclipseHelpIndexFolder.nativePath);
				Alert.show("Sorry, the Eclipse help folder has not been found: " + eclipseHelpIndexFolder.nativePath,
				"Error: Eclipse help index was not deteled", Alert.OK, null, onEclipseHelpFolderDoesNotExistAlertClose, null, Alert.OK);
			}
		}
		
		private function onEclipseHelpFolderDoesNotExistAlertClose(event:CloseEvent): void
		{
			finalizeCleaning();
		}
		
		private function onEclipseHelpFolderDeletionComplete(event:Event): void
		{
			Output.append("Eclipse help index folder is deleted. " +
				"\n\nNow, restart Flash Builder to see the new help contents " +
				"in the standard Eclipse's help search and in Help > Eclipse Help. " +
				"During the first search, Eclipse help will be re-indexed to make new contents searchable.\n\n" +
				"This application can be closed now.");
			finalizeCleaning();
		}
		
		private function onEclipseHelpFolderDeletionIOError(event:IOErrorEvent ): void
		{
			Output.append("IO Error occured during the Eclipse help index folder deletion. " +
				"Error text says: " + event.text);
			finalizeCleaning();
		}
		
		private function finalizeCleaning(): void
		{
			eclipseHelpIndexFolder.removeEventListener(Event.COMPLETE, onEclipseHelpFolderDeletionComplete, false);
			eclipseHelpIndexFolder.removeEventListener(IOErrorEvent.IO_ERROR, onEclipseHelpFolderDeletionIOError, false);
			
			var cleanFinishedEvent:ParserEvent = new ParserEvent(ParserEvent.HELP_INDEX_CLEANED);
			FlexGlobals.topLevelApplication.dispatchEvent(cleanFinishedEvent);
		}
	}
}